package com.znufe.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.znufe.exam.domain.grade.*;
import com.znufe.exam.repository.ExamGradeMapper;
import com.znufe.exam.service.ExamGradeService;
import com.znufe.exam.utility.ModelMapperSingle;
import com.znufe.exam.viewmodel.admin.grade.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExamGradeServiceImpl implements ExamGradeService {

    private final ExamGradeMapper examGradeMapper;
    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();

    @Autowired
    public ExamGradeServiceImpl(ExamGradeMapper examGradeMapper) {
        this.examGradeMapper = examGradeMapper;
    }

    @Override
    public List<ExamPaperOfCourseResponseVM> selectExamPaperListByCourseId(Integer courseId) {
        return selectExamPaperListByCourseId(courseId);
    }

    @Override
    public List<CourseContentResponseVM> selectCourseInfoByClassId(Integer classId) {
        return selectCourseInfoByClassId(classId);
    }

    @Override
    public String selectClassNameByClassId(Integer classId) {
        return selectClassNameByClassId(classId);
    }

    @Override
    public CourseContentOfClassResponseVM getCourseContentByClassId(Integer classId) {
        CourseContentOfClassResponseVM vm = new CourseContentOfClassResponseVM();
        vm.setClassId(classId);

        String className = examGradeMapper.selectClassNameByClassId(classId);
        vm.setClassName(className);
        // 取出班级所上过的课程
        List<CourseContentResponseVM> courseContentList = examGradeMapper
                 .selectCourseInfoByClassId(classId);
        // 遍历courseContentList,找出每一个courseContent中课程对应的试卷
        for (CourseContentResponseVM courseContent : courseContentList) {
            Integer courseId = courseContent.getCourseId();
            List<ExamPaperOfCourseResponseVM> examPaperList = examGradeMapper.
                    selectExamPaperListByCourseId(courseId);
            courseContent.setExamPaperList(examPaperList);
        }
        vm.setCourseContentList(courseContentList);
        return vm;
    }

    @Override
    public List<ClassCourseGradeResponseVM> selectClassCourseGrade(ClassCourseGradeRequestVM requestVM) {
        return examGradeMapper.selectClassCourseGrade(requestVM);
    }

    @Override
    public List<ClassExamGradeResponseVM> selectClassExamGrade(ClassExamGradeRequestVM requestVM) {
        return examGradeMapper.selectClassExamGrade(requestVM);
    }

    @Override
    public Integer selectClassIdByClassName(String className) {
        return examGradeMapper.selectClassIdByClassName(className);
    }

    @Override
    public String selectCourseNameByCourseId(Integer courseId) {
        return examGradeMapper.selectCourseNameByCourseId(courseId);
    }

    @Override
    public  Integer selectCourseIdByCourseName(String courseName) {
        return examGradeMapper.selectCourseIdByCourseName(courseName);
    }

    @Override
    public List<StudentIndividualGradeResponseVM> selectStudentIndividualGrade(StudentIndividualGradeRequestVM requestVM) {
        return examGradeMapper.selectStudentIndividualGrade(requestVM);
    }

    @Override
    public String selectExamPaperNameByExamPaperId(Integer examPaperId) {
        return examGradeMapper.selectExamPaperNameByExamPaperId(examPaperId);
    }

    @Override
    public List<StudentIndividualGradeResponseVM> getStudentIndividualGradeInfo(StudentIndividualGradeRequestVM requestVM) {
        /* // 以下注释掉的代码块 是为了将查询请求VM中的名字 换成 与名字相对应的id
        // 拿到requestVM里面的stuName
        StudentIndividualGradeRequestObject obj = modelMapper
                .map(requestVM, StudentIndividualGradeRequestObject.class);

        Integer classId = examGradeMapper.selectClassIdByClassName(requestVM.getClassName());
        obj.setClassId(classId);
        // 判断前端是否输入了课程名称，若输入，则obj中要拿到课程对应的id
        if (null != requestVM.getCourseName()) {
            Integer courseId = examGradeMapper
                    .selectCourseIdByCourseName(requestVM.getCourseName());
            obj.setCourseId(courseId);
        }
        */
        // 拿到ResponseVM里所需的六个字段，还有两个字段将在for循环中获取
        List<StudentIndividualGradeResponseVM> individualGradeList = examGradeMapper
                .selectStudentIndividualGrade(requestVM);

        for (StudentIndividualGradeResponseVM individualGrade : individualGradeList) {
            Integer courseId = individualGrade.getCourseId();
            /*
            String courseName = requestVM.getCourseName() == null ?
                    examGradeMapper.selectCourseNameByCourseId(courseId) :
                    requestVM.getCourseName();
                    */
            String courseName = examGradeMapper.selectCourseNameByCourseId(courseId);
            individualGrade.setCourseName(courseName);

            Integer examPaperId = individualGrade.getExamPaperId();
            String examPaperName = examGradeMapper
                    .selectExamPaperNameByExamPaperId(examPaperId);
            individualGrade.setExamPaperName(examPaperName);
        }
        return individualGradeList;
    }

    @Override
    public StudentExamQuestionAnswerObject selectStuAns(StudentExamDetailRequestObject requestObject) {
        return examGradeMapper.selectStuAns(requestObject);
    }

    @Override
    public List<Integer> selectExamPaperIdByClassId(Integer classId) {
        return examGradeMapper.selectExamPaperIdByClassId(classId);
    }

    @Override
    public Map<Integer, String> getExamPaperNameMap(List<Integer> examPaperIds) {
        Map<Integer, String> examPaperNameMap = new HashMap<>();
        List<HashMap<String, Object>> toRealMapList = examGradeMapper
                .getExamPaperNameMap(examPaperIds);
        if (toRealMapList != null && !toRealMapList.isEmpty()) {
            for (HashMap<String, Object> map : toRealMapList) {
                Integer id = null;
                String name = null;
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    if ("key".equals(entry.getKey())) {
                        id = (Integer)entry.getValue();
                    } else if ("value".equals(entry.getKey())) {
                        name = (String)entry.getValue();
                    }
                }
                examPaperNameMap.put(id, name);
            }
        }
        return examPaperNameMap;
    }

    @Override
    public PageInfo<ExamInfoOfCourseOfClassResponseObject> selectExamInfoOfCourseOfClass(ExamInfoOfCourseOfClassRequestVM requestVM) {
        return PageHelper.startPage(requestVM.getPageNum(),requestVM.getPageSize())
                    .doSelectPageInfo(() ->
                        examGradeMapper.selectExamInfoOfCourseOfClass(requestVM));
    }

    @Override
    public ExamGradeDistributionResponseVM selectExamGradeDistribution(ExamGradeDistributionRequestVM requestVM) {
        return examGradeMapper.selectExamGradeDistribution(requestVM);
    }

    @Override
    public List<ExamQuestionScoreSituationObject> selectExamQusScoreSituation(ExamGradeDistributionRequestVM requestVM) {
        return examGradeMapper.selectExamQusScoreSituation(requestVM);
    }
}
